import wrapper from './wrapper';
import { md_five as md5 } from './encry'

/* ----------------- */
const users = [
    {
        username: 'moke',
        password: '123456',
    },
    {
        username: 'admin',
        password: '123456',
    }
];

const toMap = (array) => {
    let map = new Map();
    array.map((value) => {
        map.set(value.username, value.password);
    });
    return map;
};

/**
 * 用户数据库Map
 */
const userMap = toMap(users);

/* ------------------------- */

export default {
    'POST /auth/login':(req, res)=>{
        console.log('登录中...');
        let { username, password } = req.body;
        if( userMap.get(username) && password === md5(userMap.get(username)) ){
            console.log('登录成功...');
            res.send(wrapper.ok());
        }else{
            console.log('登录失败...');
            res.send(wrapper.no());
        }
    },
    'POST /auth/register':(req, res)=>{
        console.log('注册中...');
        let { username, password, registerCode } = req.body;
        if( registerCode === '666' ){
            userMap.set(username, password);
            res.send(wrapper.ok());
        }else{
            res.send(wrapper.no());
        }
    },
    'GET /auth/confirmName':(req, res)=>{
        console.log('确认用户名中...');
        let { username } = req.query;
        if( !!userMap.get(username) ){
            res.send(wrapper.no());
        }else{
            res.send(wrapper.ok());
        }
    }
}
